<?php

class User_manager extends Controller {

	function __construct()
	{
		parent::Controller();
		if($this -> session -> userdata('user_id') === FALSE)
			redirect("login/doLogout");	
	}
	
	function index()
	{
		$this -> load -> model('user_model');
		$data = $this -> user_model -> getUserData($this -> session -> userdata('user_id'));
		$data['page_title'] = "User management";
		$this -> load -> view('header', $data);
		$this -> load -> view('user_self', $data);
		if($data['level'] > 10)
			$this -> loadUserManagement();
		$this -> load -> view('footer');
		
	}
	
	function updatePersonalData()
	{
		$this -> load -> model('user_model');
		if($this -> input -> post('pass1') != $this -> input -> post('pass2'))
		{
			$this -> session -> set_flashdata('message', 'Passwords did not match');
			redirect('user_manager');
		}
		
		$arr['firstname'] 	= $this -> input -> post('firstname');
		$arr['surname']		= $this -> input -> post('surname');
		$arr['email']		= $this -> input -> post('email');
		if($this -> input -> post('pass1'))
			$arr['pass']		= $this -> input -> post('pass1');

		$this -> user_model -> manageUser($arr, $this -> session -> userdata('user_id'));
		$this -> session -> set_flashdata('message', 'Personal data updated');
		redirect('user_manager');
	}
	
	function newUser()
	{
		$this->load->library('email');
		$this -> load -> model('user_model');
		
		$arr['firstname'] 	= $this -> input -> post('firstname');
		$arr['surname']		= $this -> input -> post('surname');
		$arr['email']		= $this -> input -> post('email');
		$arr['pass']		= $this -> input -> post('pass');
		$arr['level']		= $this -> input -> post('level');
		$arr['login']		= $this -> input -> post('login');

		if(!$arr['firstname'] || !$arr['surname'] || !$arr['pass'] || !$arr['level'] || !$arr['login'])
		{
			$this -> session -> set_flashdata('message', 'You didnt provide enough information to inserting a user');
			redirect('user_manager');	
		}
		
		$this -> user_model -> manageUser($arr);
		$this -> session -> set_flashdata('message', 'New user added');
				
		$this->email->from('register@xxxproductions69.com', 'Register');
		$this->email->to($arr['email']); 
		
		$this->email->subject('Account created to Time Tracking System (xxxproductions69)');
		$this->email->message('
		Hello. We have created an account for you to track your hours.
		Please, log in @ '. site_url() .'
		Your username is '. $arr['firstname'] .' '. $arr['surname'] .'
		And the password is: '. $arr['pass'] .'
		
		Hopefully you enjoy the system.
		 -system administrators'); 
		
		$this->email->send();
		
		redirect('user_manager');
	}

	function manageUser()
	{
		$this -> load -> model('user_model');
		if($this -> input -> post('delete'))
		{
			$this -> user_model -> deleteUser($this -> input -> post('uid'));
			$this -> session -> set_flashdata('message', 'User deleted');
			redirect('user_manager');
		}
		$arr['firstname'] 	= $this -> input -> post('firstname');
		$arr['surname']		= $this -> input -> post('surname');
		$arr['email']		= $this -> input -> post('email');
		if($this -> input -> post('pass'))
			$arr['pass']		= $this -> input -> post('pass');
		$arr['level']		= $this -> input -> post('level');
		$arr['login']		= $this -> input -> post('login');
		
		$this -> user_model -> manageUser($arr, (int)$this -> input -> post('uid'));
		$this -> session -> set_flashdata('message', 'User updated!');
		redirect('user_manager');
	}
	
	
	private function loadUserManagement()
	{
		$this -> load -> library('table');
		$this -> load -> model('user_model');
		$arr = $this -> user_model -> getUsers();
		
		$tmpl = array(	'table_open' => '<table class="listTable" cellpadding="5" cellspacing="0">',
						'row_start' => '<tr class="even">',
						'row_alt_start' => '<tr class="odd">');
		$this -> table -> set_template($tmpl);
		$this -> table -> set_heading('Firstname', 'Surname', 'Email', 'Level', 'Login', 'Edit');
		foreach($arr as $row)
		{
			$row[] = "<a href=\"#\" onclick=\"$('.popupdiv').hide(); $('#editUser{$row['id']}').slideDown(); return false;\">Edit user</a>";
			unset($row['id']);
			$this -> table -> add_row($row);
		}
		$data['arr'] = $arr;
		$data['user_table'] = $this -> table -> generate();
		$this -> load -> view('user_table', $data);
	}
	
}